package com.example.kfcclient;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpListAdapter extends BaseExpandableListAdapter {

	
	private Context context;
	private Activity activity;
	private ArrayList<Catalogy> groups;
	private ArrayList<ArrayList<Item>> children;
	
	static class ViewHolder{
		protected EditText numberFood;
		protected CheckBox check;
		protected ImageView image;
	}
	public MyExpListAdapter(Context context,ArrayList<Catalogy> groups,ArrayList<ArrayList<Item>> children){
		this.context = context;
		this.groups = groups;
		this.children = children;
	}
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		Item item = (Item) children.get(groupPosition).get(childPosition);
		return item.getItemId();
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		 Item item = (Item) getChild(groupPosition, childPosition);
		if(convertView ==  null){
			LayoutInflater infalter = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalter.inflate(R.layout.child_row, null);
			
			final ViewHolder viewHolder = new ViewHolder();
			viewHolder.numberFood = (EditText) convertView.findViewById(R.id.numberFood);
			viewHolder.check = (CheckBox) convertView.findViewById(R.id.check);
			viewHolder.image = (ImageView) convertView.findViewById(R.id.imgChild);
			viewHolder.numberFood.addTextChangedListener(new TextWatcher() {
				
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					 Item item = (Item) viewHolder.numberFood.getTag();
					 item.setNumberFood(s.toString());
					// TODO Auto-generated method stub
					
				}
				
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
					// TODO Auto-generated method stub
					
				}
				
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					
				}
			});
			
			viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					Item item = (Item) viewHolder.check.getTag();
					item.setSelected(isChecked);
				}
			});
			viewHolder.numberFood.setTag(children.get(groupPosition).get(childPosition));
			viewHolder.check.setTag(children.get(groupPosition).get(childPosition));
			convertView.setTag(viewHolder);
		}
		else
		{
			((ViewHolder) convertView.getTag()).numberFood.setTag(children.get(groupPosition).get(childPosition));
			((ViewHolder) convertView.getTag()).check.setTag(children.get(groupPosition).get(childPosition));
		}
		TextView tv = (TextView) convertView.findViewById(R.id.titleChild);
		tv.setText("Tên: "+item.getName());
		TextView tv2 = (TextView) convertView.findViewById(R.id.priceChild);
		tv2.setText("Giá: "+item.getPrice() + " vnđ");
		
		ImageLoader imageLoader = new ImageLoader(context);
		
		ViewHolder holder = (ViewHolder) convertView.getTag();
		holder.image.setTag(item.getIcon());
		imageLoader.DisplayImage(item.getIcon(), activity, holder.image);
		holder.numberFood.setText(item.getNumberFood());
		holder.check.setChecked(item.isSelected());
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).size();
	}

	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition).getId();
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Catalogy group = (Catalogy) getGroup(groupPosition);
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.group_row, null);
		}
		TextView tv = (TextView) convertView.findViewById(R.id.groupName);
		tv.setText(" "+group.getName());
		return convertView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
}

